/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.ElevatorContract;
import org.springblade.modules.admin.service.IBuildingContractPlusService;
import org.springblade.modules.admin.service.IElevatorContractService;
import org.springblade.modules.admin.vo.contract.ContractExtVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.MaintainContractType;
import org.springblade.modules.admin.vo.MaintainContractTypeVO;
import org.springblade.modules.admin.wrapper.MaintainContractTypeWrapper;
import org.springblade.modules.admin.service.IMaintainContractTypeService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 合同保养类型 控制器
 *
 * @author ysq
 * @since 2024-03-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/maintain/contract/type")
@Api(value = "合同保养类型", tags = "合同保养类型接口")
public class MaintainContractTypeController extends BladeController {

	private final IMaintainContractTypeService maintainContractTypeService;

	private final IElevatorContractService elevatorContractService;


	//封装数据,因为数据很少变动,程序写死名称即可

	//1:部分配件2:全部配件3:不包配件
	private final static Integer[] changeTypes = {0, 1, 2, 3};

	//1:金额限制2:清单限制
	private final static Integer[] changeRestrictTypes = {0, 1, 2};

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainContractType")
	public R<MaintainContractTypeVO> detail(Long id) {
		MaintainContractType detail = maintainContractTypeService.getById(id);
		return R.data(MaintainContractTypeWrapper.build().entityVO(detail));
	}


	/**
	 * 自定义分页 合同保养类型
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入maintainContractType")
	public R<IPage<MaintainContractTypeVO>> page(MaintainContractTypeVO maintainContractType, Query query) {
		IPage<MaintainContractTypeVO> pages = maintainContractTypeService.selectMaintainContractTypePage(Condition.getPage(query), maintainContractType);
		return R.data(pages);
	}


	/**
	 * 自定义分页 合同保养类型
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表")
	public R<List<MaintainContractType>> list() {
		List<MaintainContractType> list = maintainContractTypeService.contractTypeList(new ArrayList<>(1));
		return R.data(list);
	}


	/**
	 * 新增 合同保养类型
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入maintainContractType")
	public R save(@Valid @RequestBody MaintainContractType maintainContractType) {
		MaintainContractType serviceOne = null;
		//等于部分配件
		if (maintainContractType.getChangeType().equals(changeTypes[1])) {
			if (maintainContractType.getChangeRestrictType().equals(changeRestrictTypes[1])) {
				serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()).eq(MaintainContractType::getChangeRestrictType, maintainContractType.getChangeRestrictType()).eq(MaintainContractType::getPartPricesType, maintainContractType.getPartPricesType()).eq(MaintainContractType::getPartPrices, maintainContractType.getPartPrices()));
			} else if (maintainContractType.getChangeRestrictType().equals(changeRestrictTypes[2])) {
				serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()).eq(MaintainContractType::getChangeRestrictType, maintainContractType.getChangeRestrictType()).eq(MaintainContractType::getChangeListType, maintainContractType.getChangeListType()));
			}
		} else {
			serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()));
		}
		if (serviceOne != null) {
			throw new ServiceException("列表已存在相同数据的保养类型");
		}
		//表示不可删除
		Integer status = 0;
		maintainContractType.setStatus(status);
		return R.status(maintainContractTypeService.save(maintainContractType));
	}

	/**
	 * 修改 合同保养类型
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入maintainContractType")
	public R update(@Valid @RequestBody MaintainContractType maintainContractType) {
		if (maintainContractType.getChangeType() == null) {
			throw new ServiceException("配件更换范围必选项");
		}
		MaintainContractType contractType = isExist(maintainContractType);
		if (contractType != null) {
			throw new ServiceException("列表已存在相同数据的保养类型");
		}
		return R.status(maintainContractTypeService.updateById(maintainContractType));
	}


	/**
	 * 新增 合同保养类型
	 */
	@GetMapping("/copy")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "拷贝", notes = "传入maintainContractType")
	public R copy(Long id) {
		MaintainContractType maintainContractType = maintainContractTypeService.getById(id);
		maintainContractType.setId(null);
		//表示不可删除
		Integer status = 0;
		maintainContractType.setStatus(status);
		maintainContractType.setName(maintainContractType.getName() + "-复制" + RandomUtil.randomNumbers(3));
		maintainContractTypeService.save(maintainContractType);
		return R.data(true);
	}


	/**
	 * 新增 合同保养类型
	 */
	@GetMapping("/delete")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "删除", notes = "传入maintainContractType")
	public R delete(Long id) {
		maintainContractTypeService.removeById(id);
		return R.data(true);
	}


	private MaintainContractType isExist(MaintainContractType maintainContractType) {
		MaintainContractType serviceOne = null;
		//等于部分配件
		if (maintainContractType.getChangeType().equals(changeTypes[1])) {
			if (maintainContractType.getChangeRestrictType().equals(changeRestrictTypes[1])) {
				serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()).eq(MaintainContractType::getChangeRestrictType, maintainContractType.getChangeRestrictType()).eq(MaintainContractType::getPartPricesType, maintainContractType.getPartPricesType()).eq(MaintainContractType::getPartPrices, maintainContractType.getPartPrices()));
			} else if (maintainContractType.getChangeRestrictType().equals(changeRestrictTypes[2])) {
				serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()).eq(MaintainContractType::getChangeRestrictType, maintainContractType.getChangeRestrictType()).eq(MaintainContractType::getChangeListType, maintainContractType.getChangeListType()));
			}
		} else {
			serviceOne = maintainContractTypeService.getOne(Wrappers.<MaintainContractType>lambdaQuery().eq(MaintainContractType::getName, maintainContractType.getName()).eq(MaintainContractType::getChangeType, maintainContractType.getChangeType()));
		}
		if (serviceOne != null) {
			//判断附件
			if (serviceOne.getExtUrls().size() > 0) {
				List<ContractExtVo> extUrls = JSON.parseArray(JSON.toJSONString(serviceOne.getExtUrls()), ContractExtVo.class);
				List<String> extNames1 = maintainContractType.getExtUrls().stream().map(ContractExtVo::getName).collect(Collectors.toList());
				List<String> extNames2 = extUrls.stream().map(ContractExtVo::getName).collect(Collectors.toList());
				if (extNames1.equals(extNames2)) {
					return serviceOne;
				}
			}
		}
		return null;
	}

}
